;
define(function(require) {
    'use strict';
    var init = function() {
    	if(base.getData("appKey") != null && base.getData("appKey") != "FINANCE-ENTERPRISE-WEB"){
    		base.clearData();
    		location.href = "../login/login.html";
    	}
        initEvent();
    };
    var base = require("base");
    var onPageBeforeLoad = {}, onPageAfterLoad = {};
    var initEvent = function() {

    	window.onhashchange = function(){  		
    		var actualUrl = parseHash(location.hash);
    		loadPage(actualUrl);
    	}
    	window.onload = function(){
    		if (location.hash != '') {
	    		var actualUrl = parseHash(location.hash);
	    		loadPage(actualUrl);
    		}
    	}
		// 拦截所有a标签的跳转事件，改变hash值，使用location.replace 跳转
		$("a").on("click", function(event){
			// 只有包含link类的a标签才需要拦截
			if ($(this).hasClass("link")) {
				event.preventDefault();
				var url = $(this).attr("href");
				changeHash(url);
			}
			
		});
	}

	var route = function(url, forceLoad) {
		changeHash(url, forceLoad);
	}

	var changeHash = function(url, forceLoad) {
		if (!forceLoad)
			forceLoad = true;
		var pageName = url.substring(0, url.indexOf("."));
		var params = getQueryParams(url);
		if (forceLoad)
			params.forceLoad = forceLoad;
		base.saveData(pageName, JSON.stringify(params));
		var currentHash = window.location.hash;
		currentHash = currentHash.replace("#", "");
		if (currentHash == pageName){
			// 如果hash值没有变化，直接刷新页面
			loadPage(parseHash(currentHash));
		}  else
			window.location.hash = pageName;
	}

	var parseHash = function(hash) {
		var hash = hash.replace("#", "");
		if (hash === "") {
			var pathname = location.pathname;
			hash = pathname.substring(pathname.lastIndexOf("/") + 1, pathname.indexOf("."));
		}
		// 获取参数
		var params = base.getData(hash);
		params = JSON.parse(params);
		var url = hash + ".html" ;
		if (params)
			url += (getJsonObjLength(params) > 0 ? "?" : "");
		for ( var key in params ) {
			url += (key + "=" + params[key] + "&");
		}
		if (url.indexOf("?") != -1)
			url = url.substring(0, url.length - 1);
		return url;
	}	

	// ajax 加载页面跳转
	var loadPage = function(url) {
		var pathName = url.substring(0, url.indexOf("/")+1);
		url = url.substring(url.indexOf("/")+1,url.length);
		var pageName = url.substring(0, url.indexOf("."));
		var params = JSON.parse(base.getData(pathName+pageName));
		var page = "../" + pathName + pageName + ".html";
		var jsName = "../" + pathName + pageName;
		require.async([page, jsName], function(html, js){
			html = "<div>" + html + "</div>";
			// 隐藏其他页面
			$("div.page").hide();
			var selector = "div.page[data-page='" + pageName + "']";
			var pageContent = $(html).find(selector);
			var callbackParam = {"params" : params, "url":url, "jsModule" : js};
			if ($(selector).length === 0 || (params && params.forceLoad)) { // 如果要加载的页面不存在
				if (onPageBeforeLoad[pageName])
					onPageBeforeLoad[pageName](callbackParam);
				$(selector).remove();
				$(".pages").append(pageContent);
				if (onPageAfterLoad[pageName])
					onPageAfterLoad[pageName](callbackParam);
				if (js) 
					js.init(callbackParam);
			} else {
				// 已存在则直接显示
				if (onPageBeforeLoad[pageName])
					onPageBeforeLoad[pageName](callbackParam);
				$(selector).show();
				if (onPageAfterLoad[pageName])
					onPageAfterLoad[pageName](callbackParam);
			}
			
		});
		
	}
	// 获取url参数
	var getQueryParams = function(url) {
		var params = {};
	    if (url.indexOf("?") != -1) {
	        var str = url.substring(url.indexOf("?") + 1, url.length);
	        var strs = str.split("&");
	        for(var i = 0; i < strs.length; i ++) {
	            params[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
	        }
	    }
	    return params;
	}

	var onPageBeforeLoad = function(pageName, callback) {
		onPageBeforeLoad[pageName] = callback;
	}

	var onPageAfterLoad = function(pageName, callback) {
		onPageAfterLoad[pageName] = callback;
	}

	var getJsonObjLength = function(jsonObj) {
		var length = 0;
		for (var key in jsonObj)
			length ++;
		return length;
	}
    return {
        init: init,
		route: route,
		onPageBeforeLoad : onPageBeforeLoad,
		onPageAfterLoad : onPageAfterLoad
    };
});
